package com.yy.micro.magic.data.model.base;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yy.micro.magic.data.model.converter.TypedMapConverter;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class JpaObjectWithDynamicFields extends JpaObject implements DynamicFieldSupport{
    @Column(name = "dynamic_fields")
    @JsonIgnore
    @Convert(converter = TypedMapConverter.class)
    private Map<String, Object> _dynamicFields = new HashMap<>(8);

    @JsonAnyGetter
    public Map<String, Object> getDynamicFields() {
        return _dynamicFields;
    }

    public void setDynamicFields(Map<String, Object> _dynamicFields) {
        this._dynamicFields = _dynamicFields;
    }

    @Override
    public Object getFieldValue(String fieldName) {
        return this._dynamicFields.get(fieldName);
    }

    @Override
    @JsonAnySetter
    public void setFieldValue(String fieldName, Object fieldValue) {
        this._dynamicFields.put(fieldName,fieldValue);
    }

}
